<template>
    <div class="login-page" :style="{backgroundImage: 'url(' + bgImage + ')' }">
        <el-card class="login-form">
            <el-form :model="loginForm" :rules="formRules" ref="loginForm" label-width="100px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('loginForm')">立即创建</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    import loginPng from '@/assets/login.png';
    export default {
        name: "Login",
        data() {
            return {
                loginForm: {
                    username: '',
                    password: '',
                },
                formRules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
                    ]
                },
                bgImage:loginPng,
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let user = this.loginForm;
                        if (user.username == 'admin' && user.password == '1234') {
                            sessionStorage.setItem("loginState","true");
                            this.$router.push({path:'/'});
                        }else {
                            this.$message.warning('用户名或密码错误');
                        }
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>

<style scoped>
    .login-page {
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        position: fixed;
    }
    .login-form {
        width: 400px;
        position: absolute;
        top: 30%;
        left: 50%;
        margin-left: -200px;
    }
</style>